//
//  Tweet.swift
//  Twitter
//
//  Created by Hoa.wang on 2022/4/7.
//

import ParseSwift
import SwiftUI

struct Tweet: ParseObject, Identifiable {
    
    var id: String {
        objectId ?? UUID().uuidString
    }
    
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    var originalData: Data?
    
    var caption: String?
    var uid: String?
    var likes: Int?
    var timeStamp: Date? = Date()

    var user: User?
    var didLike: Bool? = false
    
    func merge(with object: Self) throws -> Self {
        var updated = try mergeParse(with: object)
        if updated.shouldRestoreKey(\.caption, original: object) {
            updated.caption = object.caption
        }
        if updated.shouldRestoreKey(\.uid, original: object) {
            updated.uid = object.uid
        }
        if updated.shouldRestoreKey(\.likes, original: object) {
            updated.likes = object.likes
        }
        if updated.shouldRestoreKey(\.timeStamp, original: object) {
            updated.timeStamp = object.timeStamp
        }
        return updated
    }
    
}
